﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace elsimulatorcsharp
{
    public class Program : Form
    {
        readonly byte MARGIN = 10;
        readonly byte V_SPACE = 40;
        readonly byte H_SPACE = 20;

        System.Timers.Timer cycles;

        bool simPaused = false;

        Building b;
        Elevator[] elevators;
        Floor[] floors;

        Form f_elevatorDialog;
        Form f_floorDialog;

        TabControl tabBar = new TabControl();
        TabPage adminUi = new TabPage("Admin");
        TabPage simulationUi = new TabPage("Simulation");
        TabPage floorUi = new TabPage("Floor");
        TabPage elevatorUi = new TabPage("Elevator");

        TextBox tb_floors = new TextBox();
        TextBox tb_elevators = new TextBox();

        Button b_editFloors = new Button();
        Button b_editElevators = new Button();
        Button b_editSimulation = new Button();
        Button b_pauseSimulation = new Button();
        Button b_startSimulation = new Button();

        TrackBar s_pplProb = new TrackBar();
        TrackBar s_speed = new TrackBar();

        Program()
        {
            Text = "elsimulator";
            Size = new System.Drawing.Size(800, 600); 
            MinimumSize = MaximumSize = Size;

            

            

            Label l_numOfFloors = new Label();
            l_numOfFloors.Text = "Number of floors";
            l_numOfFloors.Location = new System.Drawing.Point(MARGIN, MARGIN);
            l_numOfFloors.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            Label l_numOfElevators = new Label();
            l_numOfElevators.Width = l_numOfFloors.Width = 150;
            l_numOfElevators.Location = new System.Drawing.Point(MARGIN, l_numOfFloors.Location.Y + V_SPACE);
            l_numOfElevators.Text = "Number of elevators";
            l_numOfElevators.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

            tb_elevators.Width = tb_floors.Width = 200;
            tb_floors.Location = new System.Drawing.Point(l_numOfFloors.Size.Width+l_numOfFloors.Location.X+H_SPACE, MARGIN);
            tb_floors.Text = "10";
            tb_elevators.Location = new System.Drawing.Point(tb_floors.Location.X, tb_floors.Location.Y+V_SPACE);
            tb_elevators.Text = "1";

            b_editFloors.Text = b_editElevators.Text = "Edit";
            b_editFloors.Click += new System.EventHandler(floorDialog);
            b_editFloors.Location = new System.Drawing.Point(tb_floors.Width + tb_floors.Location.X + H_SPACE, MARGIN);
            b_editElevators.Location = new System.Drawing.Point(tb_elevators.Width + tb_elevators.Location.X+H_SPACE, tb_elevators.Location.Y);
            b_editElevators.Click += new System.EventHandler(elevatorDialog);

            Label l_pplProb = new Label();
            l_pplProb.Text = "Probability (%) for People Entering";
            l_pplProb.Width = 200;
            l_pplProb.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            l_pplProb.Location = new System.Drawing.Point(450 - l_pplProb.Width, b_editElevators.Location.Y + V_SPACE*2);

            s_pplProb.Width = s_speed.Width = 750;
            s_pplProb.Minimum = 0;
            s_pplProb.Maximum = 100;
            s_pplProb.Value = 50;
            s_pplProb.Location = new System.Drawing.Point(MARGIN, l_pplProb.Location.Y + V_SPACE/2);
            s_pplProb.TickFrequency = 10;

            Label l_speed = new Label();
            l_speed.Text = "Speed (ms)";
            l_speed.Width = 200;
            l_speed.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            l_speed.Location = new System.Drawing.Point(450 - l_pplProb.Width, s_pplProb.Location.Y + V_SPACE);

            s_speed.Minimum = 1;
            s_speed.Maximum = 101;
            s_speed.Value = 30;
            s_speed.Location = new System.Drawing.Point(MARGIN, l_speed.Location.Y + V_SPACE/2);
            s_speed.TickFrequency = 10;

            b_editSimulation.Text = "Edit Simulation";
            b_editSimulation.Click += new System.EventHandler(editSimulation);
            b_editSimulation.Enabled = false;
            b_editSimulation.Location = new System.Drawing.Point(MARGIN, 500 - b_editSimulation.Height);
            b_editSimulation.Width = b_pauseSimulation.Width = b_startSimulation.Width = 250;
            b_pauseSimulation.Text = "Pause Simulation";
            b_pauseSimulation.Click += new System.EventHandler(pauseSimulation);
            b_pauseSimulation.Location = new System.Drawing.Point(b_editSimulation.Location.X+250, 500 - b_editSimulation.Height);
            b_pauseSimulation.Enabled = false;
            b_startSimulation.Text = "Start Simulation";
            b_startSimulation.Click += new System.EventHandler(startSimulation);
            b_startSimulation.Location = new System.Drawing.Point(b_pauseSimulation.Location.X + 250, 500 - b_editSimulation.Height);

            adminUi.Controls.Add(l_numOfFloors);
            adminUi.Controls.Add(l_numOfElevators);
            adminUi.Controls.Add(tb_floors);
            adminUi.Controls.Add(tb_elevators);
            adminUi.Controls.Add(b_editFloors);
            adminUi.Controls.Add(b_editElevators);
            adminUi.Controls.Add(l_pplProb);
            adminUi.Controls.Add(l_speed);
            adminUi.Controls.Add(s_pplProb);
            adminUi.Controls.Add(s_speed);
            adminUi.Controls.Add(b_editSimulation);
            adminUi.Controls.Add(b_pauseSimulation);
            adminUi.Controls.Add(b_startSimulation);

            tabBar.Location = new System.Drawing.Point(0, 0);
            tabBar.Size = new System.Drawing.Size(800, 600);
            tabBar.SelectedIndex = 0;
            tabBar.TabPages.Add(adminUi);
            
            Controls.Add(tabBar);
        }

        static void Main()
        {
            Application.Run(new Program());
        }

        void startSimulation(object sender, System.EventArgs e)
        {;
            if (Int16.Parse(tb_floors.Text) > 0 && Int16.Parse(tb_elevators.Text) > 0){
                bool[] bl = new bool[Int16.Parse(tb_floors.Text)];
                floors = new Floor[Int16.Parse(tb_floors.Text)];
                for (int i = 0; i < Int16.Parse(tb_floors.Text); i++)
                {
                        floors[i] = new Floor("Floor " + i);
                        bl[i] = true;
                    }
                elevators = new Elevator[Int16.Parse(tb_elevators.Text)];
                for (int i = 0; i < Convert.ToInt16(tb_elevators.Text); i++){
                    elevators[i] = new Elevator(Int16.Parse(tb_elevators.Text));
                }

                    simPaused = false;
                    b_editSimulation.Enabled = false;
                    
                    b = new Building(elevators, floors, (byte) s_pplProb.Value);

                    cycles = new System.Timers.Timer();
                    cycles.Elapsed += new System.Timers.ElapsedEventHandler(CycleThrough);
                    cycles.Interval = s_speed.Value;
                    cycles.Enabled = true;
                    cycles.Start();

            }
                    /*
                    floorUi = new FloorPanel(b.getElevators(), b.getFloors());

                    try {
                        elevatorUi = new ElevatorPanel(elevators, (short)b.getNumberOfFloors(), new Dimension(780,550));
                    } catch (IOException ex) {
                        Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    simulationPanel = new SimulationPanel(b.getElevators(), b.getFloors(), tabBar.getSize());
                    simScrollPane = new JScrollPane(simulationPanel);
                    simScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                    simScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                    
                    
                    
                    timerSimUi = new Timer(speedSlider.getValue(), new ActionListener() {

                        public void actionPerformed(ActionEvent ae) {
                            if (!pauseSimulation){
                                b.run();
                                simulationPanel.repaint();
                                try {
                                    floorUi.updateFloors();
                                    elevatorUi.updateElevatorPanel();
                                            //setElevatorPostitions(b.getElevatorPositions(), b.getDoorOpenings());
                                } catch (IOException ex) {
                                    Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        }
                    });
                    timerSimUi.start();
                    setAdminFields(false);
                    startButton.setEnabled(false);
                    pauseButton.setEnabled(true);

                    if (tabBar.getTabCount() == 1){
                        tabBar.addTab("Simulation", simScrollPane);
                        tabBar.addTab("Floor", floorUi);
                        tabBar.addTab("Elevator", elevatorUi);
                        tabBar.setSelectedIndex(1);
                    } else {
                        System.out.println(tabBar.getTabCount());
                        tabBar.setComponentAt(1, simScrollPane);
                        tabBar.setComponentAt(2, floorUi);
                        tabBar.setComponentAt(3, elevatorUi);
                        tabBar.setSelectedIndex(1);
                    }*/
    }
        private void CycleThrough(object source, System.Timers.ElapsedEventArgs e)
        {
            if (!simPaused)
            {
               b.Run();    /*
                                simulationPanel.repaint();
                                try {
                                    floorUi.updateFloors();
                                    elevatorUi.updateElevatorPanel();
                                            //setElevatorPostitions(b.getElevatorPositions(), b.getDoorOpenings());
                                } catch (IOException ex) {
                                    Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                                }*/
            }
        }

        void editSimulation(object sender, System.EventArgs e)
        {
            MessageBox.Show("Dot Net Perls says hello.", "How is your day going?");
        }

        void pauseSimulation(object sender, System.EventArgs e)
        {
            MessageBox.Show("Dot Net Perls says hello.", "How is your day going?");
        }

        void floorDialog(object sender, System.EventArgs e)
        {
                f_floorDialog = new Form();
                f_floorDialog.Text = "Floor set up";
                DataGridView dgv = new DataGridView();
                dgv.Location = new System.Drawing.Point(0, 0);
                f_floorDialog.Controls.Add(dgv);
                f_floorDialog.Show();
        }

        void elevatorDialog(object sender, System.EventArgs e)
        {
            f_elevatorDialog = new Form();
            f_elevatorDialog.Text = "Elevator set up";
            DataGridView dgv = new DataGridView();
            dgv.Location = new System.Drawing.Point(0, 0);
            f_elevatorDialog.Controls.Add(dgv);
            f_elevatorDialog.Show();
        }

        // test
    }
}
